package com.netsdk.lht2;

import com.netsdk.demo.util.CaseMenu;
import com.netsdk.lht2.handle.EventHandle;
import com.netsdk.lht2.handle.EventHandleUtil;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.lib.enumeration.*;
import com.netsdk.lib.utils.Initialization;
import com.netsdk.module.entity.ShiJian;
import com.sun.jna.Pointer;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

public class RealLoadPicExDemo2 extends Initialization {

    int channel= -1;

    NetSDKLib.LLong    attachHandle=new NetSDKLib.LLong(0);
    ShiJian sj = new ShiJian();
    public RealLoadPicExDemo2() throws SQLException {
    }

    /**
     * 订阅智能任务
     */
    public NetSDKLib.LLong AttachEventRealLoadPic() {
        // 先退订，设备不会对重复订阅作校验，重复订阅后会有重复的事件返回
        if(attachHandle.longValue()!=0){
            this.DetachEventRealLoadPic();
        }
        // 需要图片
        int bNeedPicture = 1;
        attachHandle = netSdk.CLIENT_RealLoadPictureEx(loginHandle, channel, EM_EVENT_TYPE.EVENT_IVS_ALL.getType(), bNeedPicture,
                RealLoadPicExDemo2.AnalyzerDataCB.getInstance(), null, null);
        if (attachHandle.longValue() != 0) {
            System.out.printf("Chn[%d] CLIENT_RealLoadPictureEx Success\n", channel);
        } else {
            System.out.printf("Chn[%d] CLIENT_RealLoadPictureEx Failed!LastError = %s\n", channel,
                    ToolKits.getErrorCode());
        }

        return attachHandle;
    }
    /**
     * 报警事件（智能）回调
     */
    private static class AnalyzerDataCB implements NetSDKLib.fAnalyzerDataCallBack {
        private static RealLoadPicExDemo2.AnalyzerDataCB instance;
        public static RealLoadPicExDemo2.AnalyzerDataCB getInstance() {
            if (instance == null) {
                instance = new RealLoadPicExDemo2.AnalyzerDataCB();
            }
            return instance;
        }

        @Override
        public int invoke(NetSDKLib.LLong lAnalyzerHandle, int dwAlarmType, Pointer pAlarmInfo, Pointer pBuffer, int dwBufSize,
                          Pointer dwUser, int nSequence, Pointer reserved) throws UnsupportedEncodingException, SQLException {
            if (lAnalyzerHandle == null || lAnalyzerHandle.longValue() == 0) {
                return -1;
            }
            System.out.println("===================================EVENT RECEIVED=======================================");
            EM_EVENT_IVS_TYPE eventType = EM_EVENT_IVS_TYPE.getEventType(dwAlarmType);
            EventHandle eventHandle = EventHandleUtil.get(eventType);
            if (eventHandle != null){
                eventHandle.handle(dwAlarmType,pAlarmInfo,pBuffer,dwBufSize,dwUser,nSequence,reserved);
            }else{
                System.out.println("没有找到对应的 handle,"+ dwAlarmType);
            }
            return 0;
        }
    }

    /**
     * 停止侦听智能事件
     */
    public void DetachEventRealLoadPic() {
        if (this.attachHandle.longValue() != 0) {
            netSdk.CLIENT_StopLoadPic(this.attachHandle);
        }
    }
    public void RunTest()
    {
        System.out.println("Run Test");
        CaseMenu menu = new CaseMenu();;
        menu.addItem((new CaseMenu.Item(this , "AttachEventRealLoadPic" , "AttachEventRealLoadPic")));
        menu.addItem((new CaseMenu.Item(this , "DetachEventRealLoadPic" , "DetachEventRealLoadPic")));

        menu.run();
    }

    public static void main(String[] args) {
        RealLoadPicExDemo2 realLoadPicExDemo= null;
        try {
            realLoadPicExDemo = new RealLoadPicExDemo2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Initialization.InitTest("10.238.138.221", 37777, "admin", "huichen123!@#");
        realLoadPicExDemo.RunTest();
        LoginOut();

    }
}